<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\service\propertyService;
use app\service\apiService;
use think\Db;
use think\db\Where;
class Property extends Common{
    //查询商户列表
    public function propertyList(){
        //根据当前登录账号查询信息
        $com = $this->pidTokenId();
        //$com['property_id']=implode(",",$com['property_id']);
        $where=new Where();
        $where['id'] = array('in',$com['property_id']);
        //查询服务商信息
        $prolist = (new propertyService())->propertyList($where);
        
        $number = count($prolist);
        $this->assign('number',$number);
        if(!empty($number)){
            foreach($prolist as $lis){
                $lis['service_id'] = (new propertyService())->serviceIdName($lis['service_id']);
                $list[] = $lis;
                $this->assign('list',$list);
            }
        }else{
            $this->assign('list',$prolist);
        }
        $this->assign('prolist',$prolist);
        return $this->fetch('property_list');
    }
    //商户添加
    public function propertyAdd(){
        //根据当前登录账号查询信息
        $com = $this->pidTokenId();
        $where=new Where();
        $where['id'] = array('in',$com['service_id']);
        //查询服务商信息
        $service = (new propertyService())->serviceList($where);
        $this->assign('service',$service);
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            /*$file = request()->file('merchant_logo');
            if(!empty($file)){
                unset($data['merchant_logo']);
                //$info = $file->rule('sha1')->move(ROOT_PATH . 'public' . DS . 'parkinglogo');
                $info = $file->rule('sha1')->move('../public/parkinglogo/');
                //var_dump($info);die;
                if($info){
                    $data['merchant_logo'] = $info->getSaveName(); 
                }
                    echo $file->getError();
            }*/
            //判断账号名称是否重复        
            $info = (new propertyService())->propertyName($data['merchant_name']);
            if($info){
                $this->error('商户已存在');
            }
            $data['time'] = date('Y-m-d H:i:s',time());
            //插入数据到数据库
            $result = (new propertyService())->propertyAdd($data);
            if($result){
                $this->success('添加商户成功',url('propertyAdd'));
            }else{
                $this->error('添加商户失败',url('propertyAdd'));
            }
        }
        return $this->fetch('property_add');
    }
    //商户修改
    public function propertyEdit(){
        //根据当前登录账号查询信息
        $com = $this->pidTokenId();
        
        $where=new Where();
        $where['id'] = array('in',$com['service_id']);
        //查询服务商信息
        $service = (new propertyService())->serviceList($where);
        $this->assign('service',$service);
        if(request()->isGet()){
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('商户不存在');
            }
            $info = (new propertyService())->propertyId($id);
            $this->assign('info',$info);
            return $this->fetch('property_edit');
        }else{
            $data = request()->post();
            $file = request()->file('merchant_logo');
            if(!empty($file)){
                unset($data['merchant_logo']);
                $info = $file->rule('sha1')->move(ROOT_PATH . 'public' . DS . 'parkinglogo');
                if($info){
                    $data['merchant_logo'] = $info->getSaveName(); 
                }
                    echo $file->getError();
            }
            $result = (new propertyService())->propertyUpdate($data);
            if($result){
                    $this->success('修改商户成功',url('propertyList'));
            }else{
                    $this->error('修改商户失败');
            }
        }
        return $this->fetch('property_edit');
    }
    
    //商户修改
    public function wechatEdit(){
        //根据当前登录账号查询信息
        $com = $this->pidTokenId();
        
        $where=new Where();
        $where['id'] = array('in',$com['service_id']);
        //查询服务商信息
        $service = (new propertyService())->serviceList($where);
        $this->assign('service',$service);
        if(request()->isGet()){
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('商户不存在');
            }
            $info = (new propertyService())->propertyId($id);
            $this->assign('info',$info);
            return $this->fetch('wechat_edit');
        }else{
            $data = request()->post();
            
            $result = (new propertyService())->propertyUpdate($data);
            if($result){
                    $this->success('修改商户成功',url('propertyList'));
            }else{
                    $this->error('修改商户失败');
            }
        }
        return $this->fetch('wechat_edit');
    }
    //商户删除
    public function propertyDel(){
        $id = request()->param('id');
		$validate = $this->validate(request()->param(),[
			'id'	=>	'require|number'
		]);
		if($validate !== true){
			$this->error($validate);
        }
        //判断是否存在该数据
		$info = (new propertyService())->propertyId($id);
		if(empty($info)){
			$this->error('该商户不存在');
		}
		$result = Db::name('property')->delete($id);
        if($result){
            $this->success('删除商户成功',url('propertyList'));
		}else{
            $this->success('删除商户失败',url('propertyList'));
		}
    }
    
    public function propertyWechat(){
        //根据当前登录账号查询信息
        $com = $this->pidTokenId();
        //$com['property_id']=implode(",",$com['property_id']);
        $where=new Where();
        $where['id'] = array('in',$com['property_id']);
        //查询服务商信息
        $prolist = (new propertyService())->propertyList($where);
        
        $number = count($prolist);
        $this->assign('number',$number);
        if(!empty($number)){
            foreach($prolist as $lis){
                $lis['service_id'] = (new propertyService())->serviceIdName($lis['service_id']);
                $list[] = $lis;
                $this->assign('list',$list);
            }
        }else{
            $this->assign('list',$prolist);
        }
        $this->assign('prolist',$prolist);
        return $this->fetch('property_wechat');
    }
    //商户上线
    public function parkingSet(){
        $id = request()->param('id');
		$validate = $this->validate(request()->param(),[
			'id'	=>	'require|number'
		]);
		if($validate !== true){
			$this->error($validate);
        }
        //判断是否存在该数据
		$info = (new propertyService())->propertyId($id);
		if(empty($info)){  
			$this->error('商户信息错误');
        }
		
        $data['merchant_name'] = $info['merchant_name'];//商户简称
        $data['merchant_service_phone'] = $info['merchant_service_phone'];//商户客服电话
        $data['account_no'] = $info['account_no'];//签约支付宝账号
        //SPI接口的调用地址url，UrlEncode编码
        $data['interface_url'] = urlencode('https://www.fqsc.zfbzhsq.com/admin/Order/order');
        //将PNG图片转换为BASE64的字符串
        $image_file = './parkinglogo/'.$info['merchant_logo'];
        $image_info = getimagesize($image_file);
        $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
        $data['merchant_logo'] = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
        $data['token'] = $info['accredit'];
        // dump($data);
        $api = new apiService();
        $result = $api->configSet(json_encode(['merchant_name' => $data['merchant_name'], 'merchant_service_phone' =>  $data['merchant_service_phone'], 'account_no' => $data['account_no'],'interface_url' => $data['interface_url'], 'merchant_logo' => $data['merchant_logo'], 'token' => $data['token']]));
        //判断是否上传成功
        if(property_exists($result,'sub_msg')){
            return json(['info' => $result->sub_msg]);
        }else{
            //修改状态
            $status['id'] = $info['id'];
            $status['status'] = 1;
            $propertystatus = (new propertyService())->propertyUpdate($status);
            if($propertystatus){
                return '上线成功';
            }else{
                return '状态修改失败';
            }
        }
    }
    //ISV系统配置查询
    public function parkingQuery(){
        $api = new apiService();
        $result = $api->configQuery();
        dump($result,true);
    }
    
    public function propertyInvoice(){
         //根据当前登录账号查询信息
        $com = $this->pidTokenId();
        //$com['property_id']=implode(",",$com['property_id']);
        $where=new Where();
        //$where['a.property_id'] = array('in',$com['property_id']);
        //查询服务商信息
        $prolist =Db::name('property_invoice')
                ->alias("a")
                ->join("property b","a.property_id=b.id")
                ->where($where)
                ->field("a.*,b.merchant_name")
                
                ->select();
        $number=Db::name('property')
                ->alias("a")
                ->where($where)->count();
        $this->assign('number',$number);
        
        $this->assign('list',$prolist);
        
        $this->assign('prolist',$prolist);
        return $this->fetch('property_invoice');
    }
    public function propertyInvoiceEdit(){
        
        if(request()->isGet()){
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('商户不存在');
            }
            $info =Db::name('property_invoice')->where("property_id",$id)->find();
            $this->assign('info',$info);
            return $this->fetch('property_invoice_edit');
        }else{
            $data = request()->post();
            
            
            $result = Db::name('property_invoice')->update($data);
            if($result){
                    $this->success('修改商户成功',url('propertyList'));
            }else{
                    $this->error('修改商户失败');
            }
        }
        
    }
}